package com.wxblockchain.prometheum.ui.trade;

import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProviders;

import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.CandleData;
import com.github.mikephil.charting.data.CandleDataSet;
import com.github.mikephil.charting.data.CandleEntry;
import com.wxblockchain.core.base.BaseFragment;
import com.wxblockchain.prometheum.R;
import com.wxblockchain.widget.chart.CandelChartView;

import java.util.ArrayList;

public class TradeFragment extends BaseFragment {

    private TradeViewModel homeViewModel;

    private CandelChartView chart;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                ViewModelProviders.of(this).get(TradeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_trade, container, false);
        chart = root.findViewById(R.id.candle_chart_test);

//        homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
//            @Override
//            public void onChanged(@Nullable String s) {
//                textView.setText(s);
//            }
//        });
        pageInit();

        return root;
    }

    private void pageInit() {
        chartInit();

        dataInit();
    }

    private void chartInit() {
        chart.getDescription().setEnabled(false);

        // if more than 60 entries are displayed in the chart, no values will be
        // drawn
        chart.setMaxVisibleValueCount(60);

        // scaling can now only be done on x- and y-axis separately
        chart.setPinchZoom(false);

        chart.setDrawGridBackground(false);

        XAxis xAxis = chart.getXAxis();
//        xAxis.setEnabled(false);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);

        YAxis leftAxis = chart.getAxisLeft();
//        leftAxis.setEnabled(false);
        leftAxis.setLabelCount(7, false);
        leftAxis.setDrawGridLines(false);
        leftAxis.setDrawAxisLine(false);

        YAxis rightAxis = chart.getAxisRight();
        rightAxis.setEnabled(false);
//        rightAxis.setStartAtZero(false);

        chart.getLegend().setEnabled(true);  // 图例显示控制，就是左下角的颜色标识
    }

    private void dataInit() {
        chart.resetTracking();

        ArrayList<CandleEntry> values = new ArrayList<>();

        for (int i = 0; i < 20; i++) {
            float multi = (11 + 1);
            float val = (float) (Math.random() * 40) + multi;

            float high = (float) (Math.random() * 9) + 8f;
            float low = (float) (Math.random() * 9) + 8f;

            float open = (float) (Math.random() * 6) + 1f;
            float close = (float) (Math.random() * 6) + 1f;

            boolean even = i % 2 == 0;

            values.add(new CandleEntry(
                    i, val + high,
                    val - low,
                    even ? val + open : val - open,
                    even ? val - close : val + close,
                    getResources().getDrawable(R.drawable.ic_home_black_24dp)
            ));
        }

        CandleDataSet set1 = new CandleDataSet(values, "Data Set");

//        set1.setDrawVerticalHighlightIndicator(false);
        set1.setDrawIcons(false);
//        set1.setAxisDependency(null);
        set1.setAxisDependency(YAxis.AxisDependency.RIGHT);
//        set1.setColor(Color.rgb(80, 80, 80));
        set1.setShadowColor(Color.YELLOW);  // 影线颜色设置
        set1.setShadowColorSameAsCandle(true);    // 影线颜色是否和矩形保持一致，如果为true这上面设置的影线颜色不会生效
        set1.setShadowWidth(0.7f);


        set1.setDecreasingColor(Color.RED);  // 阴线红色
        set1.setDecreasingPaintStyle(Paint.Style.FILL); // 阴线的填充方式
        set1.setIncreasingColor(Color.rgb(122, 242, 84));  // 阳线颜色设置
        set1.setIncreasingPaintStyle(Paint.Style.FILL);  // 阳线填充方式，空心或者实心
        set1.setNeutralColor(Color.BLUE);
        set1.setBarSpace(0.4f);
//        set1.setShowCandleBar(false);
        //set1.setHighlightLineWidth(1f);

        CandleData data = new CandleData(set1);

        chart.setData(data);
        chart.invalidate();
    }

}
